type action = {
  type: string;
  payload: any;
};
export const store = {
  captchaList: new Map(),
};

export const actions = {
  addCaptcha({ key, expires }) {
    //封装一个随机生成数字的函数
    function random(a, b) {
      var n = Math.round(Math.random() * (a - b) + b);
      return n;
    }
    function getcode() {
      //定义一个字符串把所有可能都装进去，全部字母以及数字
      var codestr =
        'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789';

      //声明一个空的字符串，用作装随机生成的四个数。
      var str = '';

      //写一个循环用来重复随机生成几个数，以及将东西一个一个放进上面声明的空字符串内
      for (var i = 0; i < 4; i++) {
        var m = random(0, 61);
        str += codestr.charAt(m);
      }
      //在外面调用函数时，一定要在函数内写返回值
      return str;
    }
    let captcha = getcode();
    store.captchaList.set(key, captcha);
    setTimeout(() => {
      delete store.captchaList[key];
    }, expires * 10);
    return store.captchaList.get(key);
  },
  getCaptcha(key) {
    return store.captchaList.get(key);
  },
};
export const effects = {
  addEmailCaptcha: () => {},
};
export const action = ({ type, payload }: action) => {
  return actions[type](payload);
};
export const effect = async ({ type, payload }: action) => {
  return await effects[type](payload);
};
